import Vue from 'vue';
import { getCityJSON, getProJSON } from '@/api/json';
import { formateData, formateDataLine, geoLine } from './common';
import { apiConfig } from './config';

export function geoJSON(code) {
  return new Promise((resolve) => {
    const codeJSON = Vue.prototype.$ss.get(code);
    if (codeJSON) {
      const faceJSON = codeJSON;
      const LineJSON = geoLine(JSON.parse(JSON.stringify(codeJSON)));
      resolve({
        faceJSON,
        LineJSON
      });
      return;
    }
    getCityJSON(apiConfig.url, code).then(res => {
      Vue.prototype.$ss.set(code, res);
      const faceJSON = res;
      const LineJSON = geoLine(JSON.parse(JSON.stringify(res)));
      resolve({
        faceJSON,
        LineJSON
      });
    });
  });
}

// 宁波市数据
// export function getCityData() {
//   return new Promise((resolve) => {
//     getCityJSON('330200').then(res => {
//       const faceJSON = res;
//       resolve({
//         faceJSON
//       });
//     });
//   });
// }

export function getProDataJSON(params) {
  return new Promise((resolve) => {
    getProJSON(apiConfig.url).then(res => {
      const faceGeo = formateData(res);

      // 线
      const lineJSON = JSON.parse(JSON.stringify(res));
      const lineGeo = formateDataLine(lineJSON);
      lineJSON.features.forEach((e, i) => {
        e.geometry.type = 'LineString';
        e.geometry.coordinates = e.geometry.coordinates[0][0];
        e.properties.type = i + 1;
      });

      // 点 文字
      const pointNameJSON = JSON.parse(JSON.stringify(res));
      // console.log(pointNameJSON, 'pointNameJSON');
      pointNameJSON.features.forEach((e, i) => {
        e.geometry.type = 'Point';
        e.properties.centroid[1] = e.properties.centroid[1] - 0.02;
        e.geometry.coordinates = e.properties.centroid;
        e.properties.type = i + 1;
      });
      // const pointNameGeo = new Loca.GeoJSONSource({
      //     data: JSON.parse(JSON.stringify(pointNameJSON))
      // });

      // const pointNameGeo = pointNameJSON;

      resolve({
        faceGeo,
        lineGeo,
        pointNameJSON
      });
    });
  });
}
